<?php

namespace app\api\controller;
use app\api\common\Common;
use app\api\common\ApiErrDesc;

class Product extends Common {

    /**
     * 小程序产品详情页数据
     * @return [json]      [产品详情数据]
     */
    public function index() {
        $data = $this->params;  // 得到请求数据
        $dataurl = $this->__DATAURL__;  // 图片前缀地址

        $product = db('product')->where('id='.$data['id'].' AND del=0 AND is_down=0')->find();
        if(!$product){
            $this->return_msg(ApiErrDesc::PRODUCTSDOWN[0], ApiErrDesc::PRODUCTSDOWN[1]);
            exit();
        }

        $product['photo_x'] = $dataurl.$product['photo_x'];
        $product['photo_d'] = $dataurl.$product['photo_d'];
        $product['brand'] = db('brand')->where('id='.$product['brand_id'])->column('name');
        $product['cat_name'] = db('category')->where('id='.$product['cid'])->column('name');

        // 图片轮播数组
        $img = explode(',', trim($product['photo_string'], ','));
        $banner = array();
        if($product['photo_string']){
            foreach($img as $k => $v){
                $banner[] = $dataurl.$v;
            }
        }else {
            $banner[] = $product['photo_d'];
        }
        $product['img_arr'] = $banner;

        // 处理产品属性
        $catlist = array();
        $commodityAttr = array(); // 产品库还剩下的产品规则
        $attrValueList = array(); // 产品所有的产品规格
        if($product['pro_buff']){ // 如果产品属性有值才进行数据组装
            $product_buff = explode(',', $product['pro_buff']);
            foreach($product_buff as $key => $val){
                $attr_name = db('attribute')->where('id='.$val)->column('attr_name');
                $guigelist = db('guige')->where('attr_id='.$val.'AND pid='.$product['id'])->field('id, name')->select();
                $ggss = array();
				$gg=array();
				foreach ($guigelist as $k => $v) {
					$gg[$k]['attrKey']=$attr_name;
					$gg[$k]['attrValue']=$v['name'];
					$ggss[] = $v['name'];
				}
				$commodityAttr[$key]['attrValueList'] = $gg;
				$attrValueList[$key]['attrKey']=$attr_name;
				$attrValueList[$key]['attrValueList']=$ggss;
            }
        }

        $content = str_replace('/minipetmrschool/Data/', $dataurl, $product['content']);
		$product['content']=html_entity_decode($content, ENT_QUOTES , 'utf-8');

		//检测产品是否收藏
		$col = db('product_sc')->where('uid='.$data['user_id'].' AND pid='.$data['id'])->column('id');
		if ($col) {
			$product['collect']= 1;
		}else{
			$product['collect']= 0;
        }
        
        $this->return_msg(ApiErrDesc::SUCCESS[0], ApiErrDesc::SUCCESS[1], array('product' => $product, 'commodityAttr' => $commodityAttr, 'attrValueList' => $attrValueList));
    }


    /**
     * 商品收藏
     * @return [json]     [收藏结果]
     */
    public function collection(){
        $data = $this->params;  // 得到请求数据
        $check = db('product_sc')->where('uid='.$data['user_id'].' AND pid='.$data['id'])->column('id');
		if ($check) {
			$res = db('product_sc')->where('id='.$check)->delete();
		}else{
			$datac = array();
			$datac['uid'] = $data['user_id'];
			$datac['pid'] = $data['id'];
			$res = db('product_sc')->add($datac);
		}
		
		if ($res) {
			$this->return_msg(ApiErrDesc::SUCCESS[0], ApiErrDesc::SUCCESS[1]);
		}else{
			$this->return_msg(ApiErrDesc::COLLECTIONERR[0], ApiErrDesc::COLLECTIONERR[1]);
		}
    }

}